package qdu.rg05.ch1_intent_adv;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;

public class Act_start extends Activity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ly_actstart);

        findViewById(R.id.btn_start).setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //startActivity(new Intent(Act_start.this, Act_end.class));
                    Intent it = new Intent(Act_start.this, Act_end.class);
                    //携带数据  键值对
//                    it.putExtra("msg1","intent消息");
//                    it.putExtra("msg2",13);
                    Bundle bd = new Bundle();
                    bd.putString("msg1","intent消息2");
                    bd.putInt("msg2",15);

                    it.putExtras(bd);
                    startActivity(it);
                }
            }
        );

        findViewById(R.id.btn_switch).setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent it = new Intent(Act_start.this, Act_return.class);
                    //
                    startActivityForResult(it, 0x123);
                }
            }
        );
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data){

        if(requestCode == 0x123 && (resultCode == 0x123 /*|| resultCode == 0*/ )) {
            //Log.i("mytag",getIntent().getStringExtra("msg"));
            try{
            Log.i("mytag", "on act result:" + data.getStringExtra("msg"));}
            catch(Exception e){
                Log.i("mytag","u may not get the right returning intent obj");
            }
        }else
            Log.i("mytag","code problem");
        //Log.i("mytag","requestCode:" + requestCode + "  resultC: " + resultCode);
    }
}
